/*
 * Team Name: Focus
 * Team Members: Diana Groun, Edward Bassan
 * File Name; register.c
 * Description: This file contains the methods a register uses
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Register.h"
#include "RegisterFile.h"
#include "global.h"

/**
 * RegisterPtr newRegister(void).
 * RegisterPtr newRegister().
 */
RegisterPtr newRegister() {
	RegisterPtr newRegister = (RegisterPtr) malloc(sizeof(Register));
	newRegister->value = 0x0;
	//Pointers to functions
	newRegister->setRegister = setRegister;
	newRegister->displayRegister = displayRegister;
	newRegister->setLOB = setLOB;
	newRegister->getLOB = getLOB;
	newRegister->getHOB = getHOB;
	newRegister->setHOB = setHOB;
	return newRegister;
}

/**
 * Sets the value of a register.
 * reg - the registerPtr.
 * value - the value for the register
 */
int setRegister(RegisterPtr reg, unsigned short value) {
	if (reg == NULL ) {
		return NULL_POINTER_ERROR;
	} else {
		reg->value = value;
	}
	return NO_ERROR;
}
/**
 * Display the value of the register and check if the register is NULL.
 */
int displayRegister(RegisterPtr reg){
	if(reg == NULL){
		return NULL_POINTER_ERROR;
	}
	printf("%X:", reg->value);;
	return NO_ERROR;
}
/**
 * Returns the value of the register and check if the register is NULL.
 */
unsigned short RegisterGetValue(RegisterPtr reg){
	if(reg == NULL){
		return NULL_POINTER_ERROR;
	}else{
		return reg->value;
	}
}
/**
 * Sets the lower 8 bit of the 16 bit register.
 * r - the RegisterPtr.
 * b - the new byte for the register.
 */
int setLOB(RegisterPtr r, Byte b)
{
	r->value = r->value & BIT_HIGH_MASK;
	r->value = r->value + b;
	return NO_ERROR;
}
/**
 * Return the lower bit of a register.
 * r - the RegisterPtr.
 */
unsigned short getLOB(RegisterPtr r)
{
	return r->value = r->value & BIT_LOW_MASK;
}

/**
 * Sets the higher 8 bit of the 16 bit register.
 * r - the RegisterPtr.
 * b - the new byte for the register.
 */
int setHOB(RegisterPtr r, Byte b)
{
	r->value = r->value & BIT_LOW_MASK;
	r->value = r->value + b;
	return NO_ERROR;
}
/**
 * Return the Higher bit of a register.
 * r - the RegisterPtr.
 */
unsigned short getHOB(RegisterPtr r)
{
	return r->value = (r->value & BIT_HIGH_MASK);
}

//int main() {
//      RegisterPtr reg = newRegister();
//      reg->setRegister(reg,0x2123);
//      printf("%X",reg->getLOB(reg));
//      //reg->displayRegister(reg);
//      return 0;
//}
